<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\api\controller;


use cmf\controller\RestBaseController;
use think\App;
use think\facade\Db;

class HxkjController extends RestBaseController
{
    function test(){
        $newpalndid= Db::name('preplate')->where('plan_id',3354)->field('id')->where('delete_time',0)->select();
     $arr=array();
      foreach ($newpalndid as $k=>$v){
          array_push($arr,$v['id']);
      }
      $newarr=[
          6639,
          6640,
      ];
        $arr1=array();
      foreach ($newarr as $k=>$v){
          array_push($arr1,$v);
      }
        $result1 = array_diff($arr,$arr1);
//        $result2 = array_diff_assoc($arr1, $arr);
//        $result3 = array_diff_key($arr1, $arr);

        print_r($result1);
//        print_r($result2);
//        print_r($result3);
       $this->success("成功", $arr1);
    }
    public function getplan()
    {
        $id = $this->request->param("id");
        if (!$id) {
            $this->error('缺少参数');
        }
        $arr=array();
        $data = Db::name('plan')
            ->where('id', $id)
            ->where('delete_time', 0)
            ->order('add_time desc')
            ->find();
        // 移除所有等同于FALSE的值
        $data = array_filter($data);

// 自定义回调函数来过滤空字符串和null值
        $data = array_filter($data, function($value) {
            return ($value !== null && $value !== '');
        });
        if ($data['pd']>0){
            $data['pd']=$data['pd']*1000;
        }else{
            $data['pd']='';
        }
        if ($data['delivery_time']>0){
            $data['delivery_time']=$data['delivery_time']*1000;
        }else{
            $data['delivery_time']='';
        }
        $data['coil_id'] = explode(",", $data['plate_id']);
        $arr['formData']=$data;
        $proplan=Db::name('preplate')
            ->where('plan_id', $id)
            ->where('delete_time', 0)
            ->select();
$pro=array();
        foreach ($proplan as $key=>$val){
            $pro[$val['coil_id']]=$val;
        }


        $arr['plan']=$pro;

        $this->success("成功", $arr);
    }
    /**获取库位类型
     * @return void
     */

    function gettype()
    {
        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');
//        $data= array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库')
        $data = array();
        $num = 0;
        foreach ($type as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $k;
            $data[$num]['text'] = $v;
            $num++;
        }

        $this->success("成功", $data);

    }

    /**获取自主 来料
     * @return void
     */
    function getcompanytype()
    {
        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');
        $data = array();
        $num = 0;
        foreach ($companytypea_arr as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $k;
            $data[$num]['text'] = $v;
            $num++;
        }

        $this->success("成功", $data);
    }

    /**获取牌号
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function steelgrade()
    {
        $name = $this->request->param("name");
        if (trim($name)) {
            $steel_grade_id_arr = Db::name('grade')
                ->where('name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $steel_grade_id_arr = Db::name('grade')->where('delete_time', 0)->select();
        }

//        $data = array();
//        $num = 0;
//        foreach ($steel_grade_id_arr as $k => $v) {
////            var_dump($v);
//            $data[$num]['value'] = $v['id'];
//            $data[$num]['text'] = $v['name'];
//            $num++;
//        }
        $this->success("成功", $steel_grade_id_arr);
    }
    public function getpartno()
    {
        $name = $this->request->param("name");
        if (trim($name)) {
            $where = [];
            $fields = ['name', 'drawingno']; // field1, field2, field3替换为你需要搜索的字段名称
            foreach ($fields as $field) {
                array_push($where, [$field, 'like', '%' . trim($name) . '%']);
            }
            $steel_grade_id_arr = Db::name('partno')
                ->whereOr($where)
//                ->where('name', 'like', '%' . trim($name) . '%')
//                ->where('drawingno', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $steel_grade_id_arr = Db::name('partno')->where('delete_time', 0)->select();
        }

//        $data = array();
//        $num = 0;
//        foreach ($steel_grade_id_arr as $k => $v) {
////            var_dump($v);
//            $data[$num]['value'] = $v['id'];
//            $data[$num]['text'] = $v['name'];
//            $num++;
//        }
        $this->success("成功", $steel_grade_id_arr);
    }

    public function getcompany()
    {
        $name = $this->request->param("name");
        if ($name) {
            $company = Db::name('company')
                ->where('name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $company = Db::name('company')
                ->where('delete_time', 0)->select();

        }
        $this->success("成功", $company);
    }

    public function getware()
    {
        $user_id = $this->getUserId();
        $name = $this->request->param("name");
        if ($name) {
            $company = Db::name('ware')
                ->where('warehouse_name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $company = Db::name('ware')
                ->where('delete_time', 0)->select();

        }
        $data = array();
        $num = 0;
        foreach ($company as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $v['warehouse_id'];
            $data[$num]['text'] = $v['warehouse_name'];
            $num++;
        }
        $this->success("成功", $data);
    }

    public function getlist()
    {
        $user_id = $this->getUserId();

    }

    public function get_plate_one()
    {
        $id = $this->request->param("id");
        if (!$id) {
            $this->error('缺少参数');
        }
        $plate = Db::name('plate')
            ->where('id', $id)
            ->where('delete_time', 0)
            ->order('add_time desc')
            ->select();
//        var_dump(Db::name('plate')->getLastSql());

        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');

        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');

        $company_id_arr = Db::name('company')
            ->column('name', 'id');

        $ht_id_arr = Db::name('ht')
            ->column('ht_number', 'id');

        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');

        $steel_grade_id_arr = Db::name('steel_grade')
            ->column('name', 'id');

        $plate_id_arr = Db::name('plate')
            ->column('name', 'id');
        $coil_id_arr_da = Db::name('coil')
            ->column('coilno', 'id');
        $data = array();
        foreach ($plate as $k => $v) {
            $data[$k] = $v;
            $data[$k]['weight'] = $v['weight'] * 1000;

            $data[$k]['update_time'] = date('Y-m-d', $v["update_time"]);

//类型
            if ($v["processes"]==null) {
                $data[$k]['processes']='';
            }
            if ($v["ware_id"]==null) {
                $data[$k]['ware_id']='';
            }
            $data[$k]['grade_id'] = isset($grade_id_arr[$v['grade_id']]) ? $grade_id_arr[$v['grade_id']] : '';
            $data[$k]['steel_id'] = isset($steel_grade_id_arr[$v['steel_id']]) ? $steel_grade_id_arr[$v['steel_id']] : '';
            $data[$k]['coil_id'] = isset($coil_id_arr_da[$v['coil_id']]) ? $coil_id_arr_da[$v['coil_id']] : '';
            $data[$k]['ht_id'] = isset($ht_id_arr[$v['ht_id']]) ? $ht_id_arr[$v['ht_id']] : '';
            $data[$k]['company_id'] = isset($company_id_arr[$v['company_id']]) ? $company_id_arr[$v['company_id']] : '华星科技';
            $data[$k]['parent_id'] = isset($plate_id_arr[$v['parent_id']]) ? $plate_id_arr[$v['parent_id']] : '';
            $data[$k]['admin_id'] = isset($user_arr[$v['admin_id']]) ? $user_arr[$v['admin_id']] : '';
            $data[$k]['companytype'] = isset($companytypea_arr[$v['companytype']]) ? $companytypea_arr[$v['companytype']] : '';

            if ($v['type'] != 1 && $v['type'] != 6) {
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';
            } else {

//                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] .  $data[$k]['grade_id'] : '';
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';

            }
            $data[$k]['d'] = floatval($v['d']);


        }
        $this->success("成功", $data);
    }

    public function get_plate()
    {
        $plate = Db::name('plate')
            ->where('delete_time', 0)
            ->order('id desc')
            ->limit(1, 100)->select();

        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');

        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');

        $company_id_arr = Db::name('company')
            ->column('name', 'id');

        $ht_id_arr = Db::name('ht')
            ->column('ht_number', 'id');

        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');

        $steel_grade_id_arr = Db::name('steel_grade')
            ->column('name', 'id');

        $plate_id_arr = Db::name('plate')
            ->column('name', 'id');
        $coil_id_arr_da = Db::name('coil')
            ->column('coilno', 'id');
        $data = array();
        foreach ($plate as $k => $v) {
            $data[$k] = $v;
            $data[$k]['weight'] = $v['weight'] * 1000;

            $data[$k]['update_time'] = date('Y-m-d', $v["update_time"]);

//类型
            if ($v["processes"]==null) {
                $data[$k]['processes']='';
            }
            if ($v["ware_id"]==null) {
                $data[$k]['ware_id']='';
            }

            $data[$k]['grade_id'] = isset($grade_id_arr[$v['grade_id']]) ? $grade_id_arr[$v['grade_id']] : '';
            $data[$k]['steel_id'] = isset($steel_grade_id_arr[$v['steel_id']]) ? $steel_grade_id_arr[$v['steel_id']] : '';
            $data[$k]['coil_id'] = isset($coil_id_arr_da[$v['coil_id']]) ? $coil_id_arr_da[$v['coil_id']] : '';
            $data[$k]['ht_id'] = isset($ht_id_arr[$v['ht_id']]) ? $ht_id_arr[$v['ht_id']] : '';
            $data[$k]['company_id'] = isset($company_id_arr[$v['company_id']]) ? $company_id_arr[$v['company_id']] : '华星科技';
            $data[$k]['parent_id'] = isset($plate_id_arr[$v['parent_id']]) ? $plate_id_arr[$v['parent_id']] : '';
            $data[$k]['admin_id'] = isset($user_arr[$v['admin_id']]) ? $user_arr[$v['admin_id']] : '';
            $data[$k]['companytype'] = isset($companytypea_arr[$v['companytype']]) ? $companytypea_arr[$v['companytype']] : '';

            if ($v['type'] != 1 && $v['type'] != 6) {
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';
            } else {

//                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] .  $data[$k]['grade_id'] : '';
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';

            }
            $data[$k]['d'] = floatval($v['d']);


        }
        $this->success("成功", $data);
    }

    public function getdd()
    {
        $this->success('', 'command.setSize(70, 52)
				command.setGap(0)
				command.setCls()
				// command.setText(0, 30, "TSS24.BF2", 1, 1, "图片")
				command.setQR(40, 260, "L", 5, "A", item.name)
				// command.setText(60, 90, "TSS24.BF2", 1, 1, "佳博智汇")
				command.setText(100, 20, "TSS24.BF2", 1, 1, "山东华星新材料科技有限公司")
				command.setText(20, 50, "TSS24.BF2", 1, 1, " Shandong Huaxing New Material Tech Co., Ltd")
				command.setText(170, 100, "TSS24.BF2", 1, 1, "捆包号："+item.name)
				command.setText(170, 140, "TSS24.BF2", 1, 1, "牌号："+item.grade_id)
				command.setText(170, 180, "TSS24.BF2", 1, 1, "库位："+item.kuwei)
				command.setText(170, 220, "TSS24.BF2", 1, 1, "规格："+item.d+' * '+item.w+' * '+item.h)
				command.setText(170, 260, "TSS24.BF2", 1, 1, "张数："+item.sheets+"   理重(KG)："+item.weight)
				command.setText(170, 300, "TSS24.BF2", 1, 1, "库区："+item.name)
				command.setText(170, 340, "TSS24.BF2", 1, 1, "来料："+item.companytype)
				command.setText(170, 380, "TSS24.BF2", 1, 1, "所属公司："+item.company_id)');
    }

    /**物料添加
     * @return void
     */
    public function addplate()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        $params['update_time'] = time();
        $params['add_time'] = time();
        $params['admin_id'] = $this->getUserId();


        $id = Db::name('plate')->insertGetId($params);
        $params['id'] = $id;
        cmf_install_log($params, 'plate_log', 'add');
        if ($id) {
//            $id = input('get.id/d');

            if (!(is_numeric($id) && $id > 0)) {
                $this->error('非法参数');
            }
            $param = Db::name('Plate')->where('id', $id)->find();
            $name = getname($param['type']);
//        var_dump($name);
            Db::name('Plate')->where('id', $id)->update(array('name' => $name));
            $params = Db::name('Plate')->where('id', $id)->find();
            cmf_install_log($params, 'plate_log', 'update');

            $this->success("成功", $id);
        } else {
            $this->error('新增失败');
        }
//        $this->success('操作成功', 'index');
    }
    function update(){
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));
        $data=array( "appid"=>"__UNI__360E35E",
        "version"=>"1.0.3",
        "name"=> "华星科技",
        "imei"=>"",
        "os"=> "Android",
            "wgtUrl"=>cmf_get_file_download_url("admin/20240322/81c0cee074084d71a9df22096bd25626.apk")
        );
        if($params['appid']==$data['appid']&&$params['version']==$data['version']){
            $this->error('无更新');
        }else{
            $this->success('成功',$data);
        }

    }

}
